﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class EditPrjtElementFrm : BaseFrm
    {

        /// <summary>
        /// 传入产品元件Id
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 产品工程工序Id
        /// </summary>
        public int PrjtWorkstageId { get; set; }
        /// <summary>
        /// 工程工序加工顺序
        /// </summary>
        public int? WorkstageSort { get; set; }
        /// <summary>
        /// 所属产品工程Id
        /// </summary>
        public int PrjtId { get; set; }
        /// <summary>
        /// 产品工程代号
        /// </summary>
        public string PrjtCode { get; set; }
        /// <summary>
        /// 当前是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        /// <summary>
        /// 当前选中工程元件
        /// </summary>
        public XPrjtElement PrjtElement { get; set; }

        /// <summary>
        /// 操作状态
        /// </summary>
        Common.BillOperateType _OperateType = Common.BillOperateType.None;
        /// <summary>
        /// 操作状态
        /// </summary>
        Common.BillOperateType OperateType
        {
            get
            {
                return this._OperateType;
            }
            set
            {
                this._OperateType = value;
                initBtns();
            }
        }

        public EditPrjtElementFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
        }

        /// <summary>
        /// 绑定左边表格
        /// </summary>
        private void bindDgv()
        {
            this.dataGridView1.Rows.Clear();
            XPrjtElement[] ary = this.BaseDataProxy.FindPrjtElements(this.PrjtWorkstageId);
            if (ary != null)
            {
                int rowId = 0;
                foreach (XPrjtElement p in ary)
                {
                    this.dataGridView1.Rows.Add(new object[] { p.PrjtElement.id, p.PrjtElement.sort, p.PrjtElement.materialCode, p.MaterialName, p.PrjtElement.version });
                    if (p.PrjtElement.projectWorkstage == this.Id)
                        this.dataGridView1.Rows[rowId].Selected = true;
                    rowId++;
                }
            }

        }

        /// <summary>
        /// 初始化默认数据
        /// </summary>
        private void initData()
        {
            this.txtBoxDosage.Text = "1";
            this.txtBoxFixWastage.Text = "0.00";
            this.txtBoxWastageRate.Text = "0.00";
        }

        /// <summary>
        /// 绑定选中的数据
        /// </summary>
        private void bindData()
        {
            this.PrjtElement = this.BaseDataProxy.GetXPrjtElement(this.Id);

            if (this.PrjtElement != null)
            {

                SelectItem item = new SelectItem();
                item.Name = this.PrjtElement.MaterialName;
                item.Key = this.PrjtElement.PrjtElement.materialCode;
                item.Value = this.PrjtElement.PrjtElement.material.ToString();
                this.slcrMaterial.SetSelected(item);
                this.txtBoxMtrlName.Text = item.Name;
                this.txtBoxExpire.Text = this.PrjtElement.Expire.ToString();
                this.txtBoxSpecs.Text = this.PrjtElement.Specs;
                this.txtBoxUnit.Text = this.PrjtElement.Unit;
                bindVersion(Convert.ToInt32(this.PrjtElement.PrjtElement.material), this.PrjtElement.PrjtElement.version);

                this.txtBoxDosage.Text = this.PrjtElement.PrjtElement.dosage.ToString();
                this.txtBoxFixWastage.Text = this.PrjtElement.PrjtElement.fixWastage.ToString();
                this.txtBoxWastageRate.Text = this.PrjtElement.PrjtElement.wastageRate.ToString();
                this.cbBoxType.Text = this.PrjtElement.PrjtElement.type;
                this.cbBoxIsRelease.Checked = this.PrjtElement.PrjtElement.isRelease;
                this.cbBoxSource.Text = this.PrjtElement.PrjtElement.source;
                this.txtBoxSort.Text = this.PrjtElement.PrjtElement.sort.ToString();
                this.txtBoxNextPrjt.Text = this.PrjtElement.PrjtElement.nextPrjtCode;

                TB_ProductProject pp = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(this.PrjtElement.PrjtId));
                if (pp != null)
                    setBtnVisible(!pp.isSubmit);
            }
        }

        /// <summary>
        /// 设置窗体按钮显示状态
        /// </summary>
        /// <param name="enable"></param>
        private void setBtnVisible(bool visible)
        {
            this.tsBtnAdd.Enabled = visible;
            this.tsBtnSave.Enabled = visible;
            this.btnDelete.Visible = visible;
        }

        /// <summary>
        /// 初始化操作按钮状态
        /// </summary>
        private void initBtns()
        {
            this.tsBtnAdd.Enabled = (this.OperateType != Common.BillOperateType.Add) && (this.OperateType != Common.BillOperateType.Update);
            this.tsBtnSave.Enabled = this.OperateType != Common.BillOperateType.View;
            this.btnDelete.Enabled = this.OperateType != Common.BillOperateType.Add;
        }


        /// <summary>
        /// 绑定元件版本号
        /// </summary>
        /// <param name="elementId">元件id</param>
        private void bindVersion(int elementId, string selected)
        {
            this.cbBoxVersion.Items.Clear();
            XPrdtPrjt[] prjts = this.BaseDataProxy.GetXPrdtPrjts(elementId);
            if (prjts != null)
            {
                foreach (XPrdtPrjt p in prjts)
                    this.cbBoxVersion.Items.Add(p.PrdtPrjt);

            }
            this.cbBoxVersion.DisplayMember = "version";
            this.cbBoxVersion.ValueMember = "id";
            if (!string.IsNullOrEmpty(selected))
                this.cbBoxVersion.Text = selected;
        }

        private void EditPrjtElement_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Common.BindOptionComboBox(this.cbBoxSource, GRWL.BZSC.Common.EnumCommon.OptionType.来源类型, this.BaseDataProxy, string.Empty);
            Common.BindOptionComboBox(this.cbBoxType, GRWL.BZSC.Common.EnumCommon.OptionType.选项码, this.BaseDataProxy, string.Empty);
            if (this.EditMode && this.Id > 0)
            {
                bindData();
            }
            else
            {
                this.txtBoxSort.Text = this.BaseDataProxy.GetPrdtPrjtSerialNo((int)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSerialNoType.Element, this.PrjtWorkstageId);
                initData();
            }
            bindDgv();
            this.txtBoxWorkstageSort.Text = this.WorkstageSort.ToString();
            this.txtBoxPrjtCode.Text = this.PrjtCode;
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            this.dataGridView1.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(e.RowIndex + 1);//显示行号，也可以设置成显示其他信息 
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null)
            {
                this.EditMode = true;
                this.Id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
                bindData();
                this.OperateType = Common.BillOperateType.None;
            }
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            this.EditMode = false;
            this.Id = 0;
            this.slcrMaterial.SetSelected(null);
            this.txtBoxMtrlName.Text = string.Empty;
            this.txtBoxExpire.Text = string.Empty;
            this.txtBoxNextPrjt.Text = string.Empty;
            this.txtBoxSort.Text = this.BaseDataProxy.GetPrdtPrjtSerialNo((int)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSerialNoType.Element, this.PrjtWorkstageId);
            this.txtBoxSpecs.Text = string.Empty;
            this.txtBoxUnit.Text = string.Empty;
            initData();
            this.OperateType = Common.BillOperateType.Add;
        }

        private void slcrMaterial_SelectorClick(object sender, EventArgs e)
        {
            FindMaterialFrm frm = new FindMaterialFrm();
            frm.StockNullable = true;
            frm.Text = "选择元件";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.SelectorItem != null)
                {
                    SelectItem item = new SelectItem();
                    item.Key = frm.SelectorItem.Code;
                    item.Name = frm.SelectorItem.CnName;
                    item.Value = frm.SelectorItem.Id.ToString();
                    this.slcrMaterial.SetSelected(item);
                    this.txtBoxMtrlName.Text = item.Name;
                    this.txtBoxExpire.Text = frm.SelectorItem.Expire.ToString();
                    this.txtBoxSpecs.Text = frm.SelectorItem.Specs;
                    this.txtBoxUnit.Text = frm.SelectorItem.Unit;
                    bindVersion(frm.SelectorItem.Id, string.Empty);
                }
            }
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (this.slcrMaterial.CurrentSelectItem == null)
            {
                MessageBox.Show("请选择物料！");
                this.slcrMaterial.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxDosage.Text.Trim()))
            {
                MessageBox.Show("请输入用量！");
                this.txtBoxDosage.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxFixWastage.Text.Trim()))
            {
                MessageBox.Show("请输入固定损耗！");
                this.txtBoxFixWastage.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxWastageRate.Text.Trim()))
            {
                MessageBox.Show("请输入损耗率！");
                this.txtBoxWastageRate.Focus();
                return;
            }

            XPrjtElement xpe = new XPrjtElement();
            xpe.PrjtElement = new TB_ProjectElement();
            xpe.PrjtElement.dosage = Convert.ToDecimal(this.txtBoxDosage.Text);
            xpe.PrjtElement.fixWastage = Convert.ToDecimal(this.txtBoxFixWastage.Text);
            if (this.slcrMaterial.CurrentSelectItem != null)
            {
                xpe.PrjtElement.material = Convert.ToInt32(this.slcrMaterial.SelectedValue());
                xpe.PrjtElement.materialCode = this.slcrMaterial.CurrentSelectItem.Key;
                xpe.MaterialName = this.slcrMaterial.CurrentSelectItem.Name;
            }
            xpe.PrjtElement.version = this.cbBoxVersion.Text;

            xpe.PrjtElement.source = this.cbBoxSource.Text;
            xpe.PrjtElement.type = this.cbBoxType.Text;
            xpe.PrjtElement.wastageRate = Convert.ToDecimal(this.txtBoxWastageRate.Text);
            xpe.PrjtElement.isRelease = this.cbBoxIsRelease.Checked;
            if (this.cbBoxVersion.SelectedItem != null)
            {
                TB_ProductProject obj = (TB_ProductProject)this.cbBoxVersion.SelectedItem;
                //xpe.PrjtElement.nextPrjt = obj.id;
                xpe.PrjtElement.version = obj.version;
            }

            xpe.PrjtId = this.PrjtId;
            xpe.PrjtCode = this.PrjtCode;
            xpe.PrjtElement.projectWorkstage = this.PrjtWorkstageId;

            bool ok = false;
            Common.OperateType opType = Common.OperateType.Add;
            if (this.EditMode)
            {
                xpe.PrjtElement.deleteFlag = this.PrjtElement.PrjtElement.deleteFlag;
                xpe.PrjtElement.id = this.PrjtElement.PrjtElement.id;
                xpe.PrjtElement.sort = this.PrjtElement.PrjtElement.sort;
                xpe.PrjtElement.nextPrjt = this.PrjtElement.PrjtElement.nextPrjt;
                xpe.PrjtElement.nextPrjtCode = this.PrjtElement.PrjtElement.nextPrjtCode;
                ok = this.BaseDataProxy.UpdatePrjtElement(xpe.PrjtElement);
                if (ok)
                {
                    if (this.dataGridView1.SelectedRows != null)
                    {
                        this.dataGridView1.SelectedRows[0].SetValues(new object[] { xpe.PrjtElement.id, xpe.PrjtElement.sort, xpe.PrjtElement.materialCode, xpe.MaterialName, xpe.PrjtElement.version });
                    }
                }
                opType = Common.OperateType.Update;
            }
            else
            {
                xpe.PrjtElement.sort = Convert.ToInt32(this.txtBoxSort.Text);
                int newId = this.BaseDataProxy.AddPrjtElement(xpe.PrjtElement);
                xpe.PrjtElement.id = newId;
                ok = newId > 0;
                if (ok)
                    this.dataGridView1.Rows.Add(new object[] { xpe.PrjtElement.id, xpe.PrjtElement.sort, xpe.PrjtElement.materialCode, xpe.MaterialName, xpe.PrjtElement.version });
            }
            if (ok)
            {
                this.OperateType = Common.BillOperateType.View;
                CommonEventArg<XPrjtElement> arg = new CommonEventArg<XPrjtElement>(xpe, opType);
                GRWL.BZSC.Common.EventCommon.OnPrjtElementUdTVEvnt(sender, arg);
            }
        }

        private void tsBtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null && this.dataGridView1.SelectedRows.Count > 0 && this.PrjtElement != null)
            {
                if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.BaseDataProxy.DeletePrjtElement(this.Id))
                    {
                        this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                        CommonEventArg<XPrjtElement> arg = new CommonEventArg<XPrjtElement>(this.PrjtElement, Common.OperateType.Delete);
                        GRWL.BZSC.Common.EventCommon.OnPrjtElementUdTVEvnt(sender, arg);
                    }
                }
            }
        }

        private void slcrMaterial_SelectorTextUpdate(object sender, EventArgs e)
        {
            QMaterial[] datas = this.BaseDataProxy.FindMaterials(null, null, null, null, null, this.slcrMaterial.GetComboBoxText(), null, null, false, null);
            this.slcrMaterial.ReBindComboBox<QMaterial>(datas, "Id", "CnName", "Code");
        }

    }
}
